import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.After;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;


public class DDF3 {
	int xRows, xCols; 
	WebDriver driver;
	
	@Test
	public void myMainTest() throws Exception{
		//Local variables to store the Test Data
		String vTDID, vURL, vSearchTerm, vEmail,vPswd, vSearchResults, vWrongEmailMsg, vWelcomeMsg, vExecute;
		long iWait;
		String oSearchResults;

	
		// Read each set of Test Data from the excel
		String xlPath = "C:/Selenium/A16_2012/YT_Playing Around.xls";
		String[][] myXL = readXL(xlPath, "Test Data");
		System.out.println("Rows are " + xRows);
		// See what has been read from the Excel
		for (int i=0; i<xRows; i++) {
			for (int j=0; j<xCols; j++){
				System.out.println(myXL[i][j]);
			}
		}
		
		// Create a driver to run automation
		driver = new InternetExplorerDriver();
		iWait = 1000;
		
		// Take values from each row in 2D local array "myXL" and load them into local variables
		for (int k=1; k<xRows; k++){
			// identify the TD to execute
			vExecute = myXL[k][8];
			// Only execute if Y
			if (vExecute.equals("Y")){
					vTDID = myXL[k][0];
					vURL  = myXL[k][1];
					vSearchTerm  = myXL[k][2];
					vEmail  = myXL[k][3];
					vPswd = myXL[k][4];
					vSearchResults = myXL[k][5];
					vWrongEmailMsg  = myXL[k][6];
					vWelcomeMsg = myXL[k][7];
					System.out.println("Row is " + k);
					System.out.println("URL is " + vURL);
		
					// Execute the Test
					// TC001 
					// To youtube.com
						driver.navigate().to(vURL);
					// Enter a search term - from Excel data
						driver.findElement(By.xpath("//input[@id='masthead-search-term']")).sendKeys(vSearchTerm);
					//	Click the search button or image
						driver.findElement(By.xpath("//button[@id='search-btn']")).click();
						Thread.sleep(iWait);
						oSearchResults = driver.findElement(By.xpath("//p[@class='num-results']")).getText();
						System.out.println("The search numbers are : " + oSearchResults);
						
					//	Verify that the results show as exepcted
						
						if (vSearchResults.equals(oSearchResults)){
							System.out.println("PASSSSSSS");
						} else {
							System.out.println("FAILLLLLLLL");
						}
						
						// TC002
						//						Go To youtube.com
						//						Click the "Sign In" button
						//						Enter Email
						//						Enter Pswd
						//						Click Sign in button
						//						Confirm error message
						//						The username or password you entered is incorrect.

						
			} else {
				System.out.println("Row is " + k + " is skipped");
			}
		}
	
		
	}
	
	@After
	public void myAfter(){
		driver.quit();
	}
	
	// CUSTOM METHODS ARE HERE
	// Method to read XL
	public String[][] readXL(String sPath, String iSheet) throws Exception{

		String[][] xData;                                                                
		
		File myxl = new File(sPath);                                
		FileInputStream myStream = new FileInputStream(myxl);                                
		HSSFWorkbook myWB = new HSSFWorkbook(myStream);                                
		HSSFSheet mySheet = myWB.getSheet(iSheet);                                 
		xRows = mySheet.getLastRowNum()+1;                                
		xCols = mySheet.getRow(0).getLastCellNum();                                
		xData = new String[xRows][xCols];        
		for (int i = 0; i < xRows; i++) {                           
				HSSFRow row = mySheet.getRow(i);
				for (int j = 0; j < xCols; j++) {                               
					HSSFCell cell = row.getCell(j);                               
					String value = cellToString(cell);                               
					xData[i][j] = value;                               
					}        
				}                                      
		return xData;
	}
	
	// Method to write into an XL

	
	//Change cell type
	public static String cellToString(HSSFCell cell) { 
		// This function will convert an object of type excel cell to a string value
		int type = cell.getCellType();                        
		Object result;                        
		switch (type) {                            
			case HSSFCell.CELL_TYPE_NUMERIC: //0                                
				result = cell.getNumericCellValue();                                
				break;                            
			case HSSFCell.CELL_TYPE_STRING: //1                                
				result = cell.getStringCellValue();                                
				break;                            
			case HSSFCell.CELL_TYPE_FORMULA: //2                                
				throw new RuntimeException("We can't evaluate formulas in Java");  
			case HSSFCell.CELL_TYPE_BLANK: //3                                
				result = "%";                                
				break;                            
			case HSSFCell.CELL_TYPE_BOOLEAN: //4     
				result = cell.getBooleanCellValue();       
				break;                            
			case HSSFCell.CELL_TYPE_ERROR: //5       
				throw new RuntimeException ("This cell has an error");    
			default:                  
				throw new RuntimeException("We don't support this cell type: " + type); 
				}                        
		return result.toString();      
		}
}
